<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login/Register</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }



        .form {
            width: 35%;
            margin: 100px auto;
            padding: 15px;
        }

        .form.hidden {
            display: none;
        }

        .form input {
            width: 100%;
            height: 40px;
            display: block;
            margin-bottom: 10px;
            border-radius: 20px;
            border: solid 1px rgba(187, 191, 196, .5);
            padding-left: 10px;
            caret-color: #3370ff;
            transition: all .2s ease;
        }

        .form input:focus {
            outline: none;
            border-color: #3370ff;
        }

        .form button {
            width: 100%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background-color: #3370ff;
            color: #fff;
            outline: none;
            border-radius: 20px;
            border: none;
        }

        .form .go {
            display: inline-block;
            color: #8f959e;
            font-size: 14px;
            margin: 10px 0 0 5px;
        }
    </style>
</head>

<body>
    <form id="register" class="form hidden">
        <input type="text" name="username" placeholder="用户名" />
        <input type="password" name="password" placeholder="密码" />
        <input type="password" name="confirm_password" placeholder="确认密码" />
        <button type="submit">注册</button>
        <a href="javascript:;" class="go" onclick="toggle()">去登录</a>
    </form>

    <form id="login" class="form">
        <input type="text" name="username" placeholder="用户名" />
        <input type="password" name="password" placeholder="密码" />
        <button type="submit">登录</button>
        <a href="javascript:;" class="go" onclick="toggle()">去注册</a>
    </form>

    <script src="./util.js"></script>
    <script>
        if (!!storage.get("user")) location.href = "./index.html";
        const login = document.querySelector("#login");
        login.addEventListener("submit", function (e) {
            e.preventDefault();
            const data = getFormData(e.target);
            for (let k in data) {
                if (!data[k].trim()) {
                    return alert(k + "不能为空");
                }
            }
            const user = userDB.findOne(data.username);
            if (user && user.password === data.password) {
                alert("登录成功！");
                storage.set("user", user);
                location.href = "./index.html"
            } else {
                alert("用户名或密码错误！");
            }
        }, false);


        const register = document.querySelector("#register");

        function toggle() {
            login.classList.toggle("hidden");
            register.classList.toggle("hidden");
            clearFormData(login);
            clearFormData(register);
        }

        register.addEventListener("submit", function (e) {
            e.preventDefault();
            const data = getFormData(e.target);
            for (let k in data) {
                if (!data[k].trim()) {
                    return alert(k + "不能为空");
                }
            }
            if (data.password !== data.confirm_password) return alert("两次密码不一致");
            const user = userDB.findOne(data.username);
            if (!!user) return alert("用户已存在");
            userDB.add(data);
            alert("注册成功！");
            toggle();

        }, false)


    </script>
</body>

</html>